package com.assessment.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.assessment.entity.Aitemavg;
import com.assessment.entity.Arole;
import com.assessment.entity.Column;
import com.assessment.service.AitemavgService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.assessment.utils.Constant.*;
import static com.assessment.utils.Constant.DELETE_FAILED;
import static com.assessment.utils.ReturnMap.failedMap;
import static com.assessment.utils.ReturnMap.successMap;
import static com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isEmpty;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鑫十一
 * @since 2021-12-15
 */
@CrossOrigin
@RestController
@Api(tags={"打分项结果"})
@RequestMapping("/admin/aitemavg")
public class AitemavgController {
    private final AitemavgService service;

    public AitemavgController(AitemavgService service) {
        this.service = service;
    }

    // 获取分页列表
    @GetMapping("/getList")
    public Map<String,Object> getList(@RequestParam(required = false, value = "searchInfo") String item)
    {
        try {
            QueryWrapper<Aitemavg> queryWrapper = new QueryWrapper<>();
            if (item != null) {
                JSONObject jsonObject = JSON.parseObject(item);
                if (!isEmpty(jsonObject.get("aname"))) {
                    queryWrapper.like("aname", jsonObject.get("aname"));
                }
                if (!isEmpty(jsonObject.get("itname"))) {
                    queryWrapper.like("itname", jsonObject.get("itname"));
                }
                if (!isEmpty(jsonObject.get("pname"))) {
                    queryWrapper.like("pname", jsonObject.get("pname"));
                }
            }
            List<Aitemavg> list = service.getAvglist(queryWrapper);
            // List<Aitemavg> list = service.list(queryWrapper);
            List<Column> columns = service.getColumns();
            Map<String, Object> res = new HashMap<>();
            res.put("columns",columns);
            res.put("data",list);
            return successMap(TABLE_SUCCESS,res);
        }catch (Exception e){
            return failedMap(TABLE_FAILED);
        }
    }

    // 保存
    @PostMapping("/save")
    public Map<String, Object> save(Aitemavg item) {
        try {
            if (service.save(item)) return successMap(SAVE_SUCCESS);
            return failedMap(SAVE_FAILED);
        }catch (Exception e){
            return failedMap(SAVE_FAILED);
        }
    }
    // 更新
    @PostMapping("/update")
    public Map<String, Object> updateById(Aitemavg item) {
        try {
            if (service.updateById(item)) return successMap(UPDATE_SUCCESS);
            return failedMap(UPDATE_FAILED);
        }catch (Exception e){
            return failedMap(UPDATE_FAILED);
        }
    }

    // 删除
    @PostMapping("/remove")
    public Map<String, Object> removeByIds(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        try {
            if (service.removeByIds(idList))  return successMap(DELETE_SUCCESS);
            return failedMap(DELETE_FAILED);
        }catch (Exception e){
            return failedMap(DELETE_FAILED);
        }
    }
}

